home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / FromTheMag / JW FLV MEDIA PLAYER 4.2 / mediaplayer.exe / player.swf / scripts / com / jeroenwijering / player / Model.as < prev    next >
Text File  |  2008-11-04  |  10KB  |  312 lines

  1. package com.jeroenwijering.player
  2. {
  3.    import com.jeroenwijering.events.ControllerEvent;
  4.    import com.jeroenwijering.events.ModelEvent;
  5.    import com.jeroenwijering.events.ModelStates;
  6.    import com.jeroenwijering.models.CameraModel;
  7.    import com.jeroenwijering.models.HTTPModel;
  8.    import com.jeroenwijering.models.ImageModel;
  9.    import com.jeroenwijering.models.RTMPModel;
  10.    import com.jeroenwijering.models.SoundModel;
  11.    import com.jeroenwijering.models.VideoModel;
  12.    import com.jeroenwijering.models.YoutubeModel;
  13.    import com.jeroenwijering.utils.Draw;
  14.    import com.jeroenwijering.utils.Stretcher;
  15.    import flash.display.Bitmap;
  16.    import flash.display.DisplayObject;
  17.    import flash.display.Loader;
  18.    import flash.display.MovieClip;
  19.    import flash.events.Event;
  20.    import flash.events.EventDispatcher;
  21.    import flash.net.URLRequest;
  22.    import flash.system.LoaderContext;
  23.    
  24.    public class Model extends EventDispatcher
  25.    {
  26.        
  27.       
  28.       public var config:Object;
  29.       
  30.       private var thumb:Loader;
  31.       
  32.       private var image:String;
  33.       
  34.       private var controller:Controller;
  35.       
  36.       private var models:Object;
  37.       
  38.       private var currentModel:String;
  39.       
  40.       private var currentURL:String;
  41.       
  42.       public var skin:MovieClip;
  43.       
  44.       public function Model(param1:Object, param2:MovieClip, param3:Controller)
  45.       {
  46.          super();
  47.          config = param1;
  48.          if(config["streamscript"])
  49.          {
  50.             config["streamer"] = config["streamscript"];
  51.          }
  52.          skin = param2;
  53.          Draw.clear(skin.display.media);
  54.          controller = param3;
  55.          controller.addEventListener(ControllerEvent.ITEM,itemHandler);
  56.          controller.addEventListener(ControllerEvent.MUTE,muteHandler);
  57.          controller.addEventListener(ControllerEvent.PLAY,playHandler);
  58.          controller.addEventListener(ControllerEvent.PLAYLIST,playlistHandler);
  59.          controller.addEventListener(ControllerEvent.QUALITY,qualityHandler);
  60.          controller.addEventListener(ControllerEvent.RESIZE,resizeHandler);
  61.          controller.addEventListener(ControllerEvent.SEEK,seekHandler);
  62.          controller.addEventListener(ControllerEvent.STOP,stopHandler);
  63.          controller.addEventListener(ControllerEvent.VOLUME,volumeHandler);
  64.          thumb = new Loader();
  65.          thumb.contentLoaderInfo.addEventListener(Event.INIT,thumbHandler);
  66.          skin.display.addChildAt(thumb,skin.display.getChildIndex(skin.display.media));
  67.          models = new Object();
  68.       }
  69.       
  70.       private function thumbLoader() : void
  71.       {
  72.          var _loc1_:String = null;
  73.          _loc1_ = playlist[config["item"]]["image"];
  74.          if(_loc1_ && _loc1_ != image)
  75.          {
  76.             image = _loc1_;
  77.             thumb.load(new URLRequest(_loc1_),new LoaderContext(true));
  78.          }
  79.       }
  80.       
  81.       private function volumeHandler(param1:ControllerEvent) : void
  82.       {
  83.          if(currentModel)
  84.          {
  85.             models[currentModel].volume(param1.data.percentage);
  86.          }
  87.       }
  88.       
  89.       private function qualityHandler(param1:ControllerEvent) : void
  90.       {
  91.          if(currentModel)
  92.          {
  93.             models[currentModel].quality(param1.data.state);
  94.          }
  95.       }
  96.       
  97.       public function get playlist() : Array
  98.       {
  99.          return controller.playlist;
  100.       }
  101.       
  102.       private function thumbHandler(param1:Event) : void
  103.       {
  104.          var evt:Event = param1;
  105.          try
  106.          {
  107.             Bitmap(thumb.content).smoothing = true;
  108.          }
  109.          catch(err:Error)
  110.          {
  111.          }
  112.          resizeHandler();
  113.       }
  114.       
  115.       public function mediaHandler(param1:DisplayObject = undefined) : void
  116.       {
  117.          Draw.clear(skin.display.media);
  118.          skin.display.media.addChild(param1);
  119.          resizeHandler();
  120.       }
  121.       
  122.       private function playlistHandler(param1:ControllerEvent) : void
  123.       {
  124.          if(currentModel)
  125.          {
  126.             stopHandler();
  127.          }
  128.          else
  129.          {
  130.             sendEvent(ModelEvent.STATE,{"newstate":ModelStates.IDLE});
  131.          }
  132.          thumbLoader();
  133.       }
  134.       
  135.       private function loadModel(param1:String) : void
  136.       {
  137.          switch(param1)
  138.          {
  139.             case "camera":
  140.                models[param1] = new CameraModel(this);
  141.                break;
  142.             case "image":
  143.                models[param1] = new ImageModel(this);
  144.                break;
  145.             case "sound":
  146.                if(config["streamer"] && config["streamer"].substr(0,4) == "rtmp")
  147.                {
  148.                   models[param1] = new RTMPModel(this);
  149.                }
  150.                else
  151.                {
  152.                   models[param1] = new SoundModel(this);
  153.                }
  154.                break;
  155.             case "video":
  156.                if(config["streamer"])
  157.                {
  158.                   if(config["streamer"].substr(0,4) == "rtmp")
  159.                   {
  160.                      models[param1] = new RTMPModel(this);
  161.                   }
  162.                   else
  163.                   {
  164.                      models[param1] = new HTTPModel(this);
  165.                   }
  166.                }
  167.                else
  168.                {
  169.                   models[param1] = new VideoModel(this);
  170.                }
  171.                break;
  172.             case "youtube":
  173.                models[param1] = new YoutubeModel(this);
  174.          }
  175.       }
  176.       
  177.       private function stopHandler(param1:ControllerEvent = undefined) : void
  178.       {
  179.          currentURL = undefined;
  180.          if(currentModel)
  181.          {
  182.             models[currentModel].stop();
  183.          }
  184.          sendEvent(ModelEvent.STATE,{"newstate":ModelStates.IDLE});
  185.       }
  186.       
  187.       private function playHandler(param1:ControllerEvent) : void
  188.       {
  189.          if(currentModel)
  190.          {
  191.             if(param1.data.state == true)
  192.             {
  193.                models[currentModel].play();
  194.             }
  195.             else
  196.             {
  197.                models[currentModel].pause();
  198.             }
  199.          }
  200.       }
  201.       
  202.       private function muteHandler(param1:ControllerEvent) : void
  203.       {
  204.          if(currentModel && param1.data.state == true)
  205.          {
  206.             models[currentModel].volume(0);
  207.          }
  208.          else if(currentModel && param1.data.state == false)
  209.          {
  210.             models[currentModel].volume(config["volume"]);
  211.          }
  212.       }
  213.       
  214.       private function seekHandler(param1:ControllerEvent) : void
  215.       {
  216.          if(currentModel)
  217.          {
  218.             models[currentModel].seek(param1.data.position);
  219.          }
  220.       }
  221.       
  222.       private function itemHandler(param1:ControllerEvent) : void
  223.       {
  224.          var _loc2_:String = null;
  225.          var _loc3_:String = null;
  226.          _loc2_ = playlist[param1.data.index]["type"];
  227.          _loc3_ = playlist[param1.data.index]["file"];
  228.          if(models[_loc2_] && _loc2_ == currentModel)
  229.          {
  230.             if(_loc3_ == currentURL && _loc2_ != "rtmp")
  231.             {
  232.                models[_loc2_].seek(playlist[param1.data.index]["start"]);
  233.             }
  234.             else
  235.             {
  236.                models[_loc2_].stop();
  237.                currentURL = _loc3_;
  238.                models[_loc2_].load();
  239.             }
  240.          }
  241.          else
  242.          {
  243.             if(currentModel)
  244.             {
  245.                models[currentModel].stop();
  246.             }
  247.             if(!models[_loc2_])
  248.             {
  249.                loadModel(_loc2_);
  250.             }
  251.             currentModel = _loc2_;
  252.             currentURL = _loc3_;
  253.             models[_loc2_].load();
  254.          }
  255.          thumbLoader();
  256.       }
  257.       
  258.       public function sendEvent(param1:String, param2:Object) : void
  259.       {
  260.          var _loc3_:String = null;
  261.          if(param1 == ModelEvent.STATE && param2.newstate != config["state"])
  262.          {
  263.             switch(param2.newstate)
  264.             {
  265.                case ModelStates.IDLE:
  266.                case ModelStates.COMPLETED:
  267.                   sendEvent(ModelEvent.TIME,{
  268.                      "position":playlist[config["item"]]["start"],
  269.                      "duration":playlist[config["item"]]["duration"]
  270.                   });
  271.                   thumb.visible = true;
  272.                   skin.display.media.visible = false;
  273.                   break;
  274.                case ModelStates.BUFFERING:
  275.                case ModelStates.PLAYING:
  276.                   _loc3_ = playlist[config["item"]]["file"].substr(-3);
  277.                   if(_loc3_ != "aac" && _loc3_ != "mp3" && _loc3_ != "m4a")
  278.                   {
  279.                      thumb.visible = false;
  280.                      skin.display.media.visible = true;
  281.                   }
  282.                   else
  283.                   {
  284.                      thumb.visible = true;
  285.                      skin.display.media.visible = false;
  286.                   }
  287.             }
  288.             param2.oldstate = config["state"];
  289.             config["state"] = param2.newstate;
  290.             dispatchEvent(new ModelEvent(param1,param2));
  291.          }
  292.          else if(param1 != ModelEvent.STATE)
  293.          {
  294.             dispatchEvent(new ModelEvent(param1,param2));
  295.          }
  296.          if(param1 == ModelEvent.META && param2.width)
  297.          {
  298.             resizeHandler();
  299.          }
  300.       }
  301.       
  302.       private function resizeHandler(param1:ControllerEvent = null) : void
  303.       {
  304.          Stretcher.stretch(skin.display.media,config["width"],config["height"],config["stretching"]);
  305.          if(thumb.content && thumb.width > 0)
  306.          {
  307.             Stretcher.stretch(thumb,config["width"],config["height"],config["stretching"]);
  308.          }
  309.       }
  310.    }
  311. }
  312.